/*
    6. Write a program in C to insert a new node at the middle of Singly Linked List. 
    Test Data and Expected Output :

    Input the number of nodes (3 or more) : 4                                                                    
    Input data for node 1 : 1                                                                                    
    Input data for node 2 : 2                                                                                    
    Input data for node 3 : 3                                                                                    
    Input data for node 4 : 4                                                                                    
                                                                                                                
    Data entered in the list are :                                                                               
    Data = 1                                                                                                     
    Data = 2                                                                                                     
    Data = 3                                                                                                     
    Data = 4                                                                                                     
                                                                                                                
    Input data to insert in the middle of the list : 5                                                           
    Input the position to insert new node : 3                                                                    
                                                                                                                
    Insertion completed successfully.                                                                             
                                                                                                                
    The new list are :                                                                                           
    Data = 1                                                                                                     
    Data = 2                                                                                                     
    Data = 5                                                                                                     
    Data = 3                                                                                                     
    Data = 4  
*/

#include <stdio.h>
#include <stdlib.h>

typedef struct node* List;
typedef struct node{
    int x;
    List next;
} Node;

List createList(int n)
{
    List newNode = NULL;
    List curNode = NULL;
    List root = NULL;
    int i;
    for(i = 0; i < n; i++){
        newNode = (List)malloc(sizeof(Node));
        if(newNode == NULL){
            return NULL;
        }
        newNode->next = NULL;
        printf("Input data for node %d : ", i);
        scanf("%d", &(newNode->x));
        if(root == NULL){
            root = newNode;
            curNode = root;
        }else{
            curNode->next = newNode;
            curNode = newNode;
        }
    }
    return root;
}

List insertNodeInMiddle(List root, int pos, int x)
{
    int curpos = 1;
    List preNode = NULL;
    List curNode = root;
    List newNode = (List)malloc(sizeof(Node));
    if(newNode == NULL){
        return NULL;
    }
    newNode->x = x;
    while(curNode->next != NULL){          
        if(curpos == pos){
            break;
        }
        preNode = curNode;
        curNode = curNode->next;     
        curpos++;      
    }
    if(curNode == root){
        newNode->next = root;
        root = newNode;
    }else{
        newNode->next = curNode;
        preNode->next = newNode;
    }

    return root;
}

void printList(List list)
{
    if(list == NULL){
        printf("List is empty!\n");
        return;
    }
    for(; list != NULL; list = list->next){
        printf("Data = %d\n", list->x);
    }
}


int main(void)
{
    List root = NULL;
    int n, x, pos;
    printf("Input the number of nodes : ");
    scanf("%d", &n);
    root = createList(n);
    printf("\nData entered in the list are : \n");
    printList(root);
    printf("Input data to insert in the middle of the list : ");
    scanf("%d", &x);
    printf("Input the position to insert new node : ");
    scanf("%d", &pos);
    root = insertNodeInMiddle(root, pos, x);
    printList(root);

    return 0;

}